package com.aidex.admin.controller.system;

import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.query.QueryGenerator;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.system.domain.SysDictType;
import com.aidex.system.service.SysDictTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典信息
 */
@RestController
@RequestMapping("/system/dict/type")
public class SysDictTypeController extends BaseController<SysDictTypeService> {

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/page")
    public R list(SysDictType entity,
                  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                  HttpServletRequest req) {
        QueryWrapper<SysDictType> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        Page<SysDictType> page = new Page<>(pageNo, pageSize);
        IPage<SysDictType> pageList = service.selectPage(page, queryWrapper);
        return R.data(pageList);
    }

    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:dict:export')")
    @GetMapping("/export")
    public R export(SysDictType entity, HttpServletRequest req) {
        QueryWrapper<SysDictType> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        List<SysDictType> list = service.selectList(queryWrapper);
        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
        return util.exportExcel(list, "字典类型");
    }

    /**
     * 查询字典类型详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable String id) {
        return R.data(service.selectById(id));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public R save(@Validated @RequestBody SysDictType entity) {
        if (StringUtils.isEmpty(entity.getId())){
            return R.status(service.insert(entity));
        }
        return R.status(service.updateById(entity));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        service.deleteBatchIds(Arrays.asList(ids));
        return R.success();
    }

    /**
     * 刷新缓存
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public R refreshCache() {
        service.refreshCache();
        return R.status(true);
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public R optionselect() {
        return R.data(service.selectList(new QueryWrapper<>()));
    }

    /**
     * 校验数据字典类型是否存在
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典类型", businessType = BusinessType.CHECK)
    @GetMapping("/checkDictTypeUnique")
    public R checkDictTypeUnique(SysDictType entity) {
        Map<String, String> checkMap = new HashMap<>(16);
        try {
            service.checkDictTypeUnique(entity);
            checkMap.put("code", "1");
        } catch (Exception e) {
            checkMap.put("code", "2");
        }
        return R.data(checkMap);
    }
}
